﻿Imports Data


Public Class ChequeForm


    Private Property id_pago As Integer



    Private Sub ChequeForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargaLV()
    End Sub


    Private Function CargaLV() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.lvCheques.Items.Clear()
                Dim lista As List(Of Cheques) = db.Cheques.Where(Function(p) p.id_factira = id_pago).Select(Function(p) p).ToList

                For Each item In lista
                    Me.lvCheques.Items.Add(New ListViewItem(New String() {item.fecha_cobrar, _
                                                                          item.monto, _
                                                                          item.Estado.nombre
                                                                          }) With {.Name = item.id})
                Next
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Sub New(ByVal id_pago As Integer)
        InitializeComponent()

        Me.id_pago = id_pago

    End Sub

    Private Sub CancelarChequeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CancelarChequeToolStripMenuItem.Click
        Try
            Me.Cancelar()
            Me.CargaLV()
        Catch ex As Exception

        End Try
    End Sub


    Private Function Cancelar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim cheque As Cheques = db.Cheques.Where(Function(p) p.id = Me.lvCheques.SelectedItems(0).Name).Select(Function(p) p).FirstOrDefault
                cheque.id_estado = 1
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Try
            Me.Eliminar(lvCheques.SelectedItems(0).Name)
        Catch ex As Exception

        End Try
    End Sub

    Private Function Eliminar(ByVal id_cheque As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim cheque As Data.Cheques = db.Cheques.Where(Function(p) p.id = id_cheque).Select(Function(p) p).FirstOrDefault
                db.Cheques.DeleteOnSubmit(cheque)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class